#!/usr/bin/env python

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import users

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template

from blog import *
from util import *
from recipes import *
from projects import *
from cv import *
from wineCellar import *

class Restaurant:
    def __init__(self):
        self.name = ""
        self.address = ""
        self.link = ""

class ShowCphBestRestaurants(webapp.RequestHandler):
    def get(self):
        pagePath = []
        pagePath.append(PageDir("restauranter", ""))

        simplenote = Simplenote("thomas.the.red@gmail.com", "32hej32")
        note = simplenote.get_note(u'agtzaW1wbGUtbm90ZXINCxIETm90ZRjlurwMDA')
        data = note[0]["content"].split("\n")
        data = data[3:]
#        notes = simplenote.get_note_list()
#        self.response.out.write(notes)
#        return

        restaurants = []
        for line in data:
            if not line.strip():
                continue
            d = line.split(", ")
            r = Restaurant()
            r.name = d[0]
            r.link = d[1]
            r.address = d[2]
            r.city = d[2]
            r.phoneNumber = d[4]
            r.mapLink = d[5]
            restaurants.append(r)

        template_values = {
            'path': pagePath,
            'restaurants': restaurants,
            'user': MyUser()
        }

        path = os.path.join(os.path.dirname(__file__), 'cphBestRestaurants.html')
        self.response.out.write(template.render(path, template_values))


class ShowIndex(webapp.RequestHandler):
    def get(self):
        pages = []
        pages.append(Page("blog", "/blog"))
        pages.append(Page("opskrifter", "/recipes"))
        pages.append(Page("links til opskrifter", "/recipeLinks"))
        #pages.append(Page("cv", "/cv", True))
        #pages.append(Page("projekter", "/projects", True))
        pages.append(Page("restauranter", "/cphBestRestaurants", True))
        pages.append(Page("vink&aelig;lder", "/wineCellar"))

        template_values = {
            'pages': pages,
            'user': MyUser()
        }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class ShowLogout(webapp.RequestHandler):
    def get(self):
        self.redirect(users.create_logout_url("/"))

class ShowLogin(webapp.RequestHandler):
    def get(self):
        self.redirect(users.create_login_url("/"))

def main():
    application = webapp.WSGIApplication([('/', ShowIndex),
                                         ('/recipes', ShowRecipes),
                                         ('/recipeLinks', ShowRecipeLinks),
                                         ('/recipe', ShowRecipe),
                                         ('/projects', ShowProjects),
                                         ('/cv', ShowCV),
                                         ('/editRecipe', EditRecipe),
                                         ('/editBlog', EditBlog),
                                         ('/deleteRecipe', DeleteRecipe),
                                         ('/cphBestRestaurants', ShowCphBestRestaurants),
                                         ('/wineCellar', ShowWineCellar),
                                         ('/editWine', EditWine),
                                         ('/showWine', ShowWine),
                                         ('/logout', ShowLogout),
                                         ('/login', ShowLogin),
                                         ('/blog', ShowBlog)],
                                         debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
